package com.px.system.service.impl;

import com.px.common.constant.Constants;
import com.px.common.core.domain.model.PhoneCode;
import com.px.common.core.redis.RedisCache;
import com.px.common.utils.StringUtils;
import com.px.system.service.IPhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 飞行用户信息Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class PhoneServiceImpl implements IPhoneService
{
    @Autowired
    private RedisCache redisCache;

    @Override
    public String sendCode(PhoneCode phoneCode) {
        String msg = "";
        switch (phoneCode.getType()){
            //注册
            case 0 :
                if(StringUtils.isNotNull(redisCache.getCacheObject(phoneCode.getPhonenumber()+Constants.REGISTER))){
                    msg = "一分钟内只能请求一次验证码，请等候~~";
                    break;
                }
//        redisCache.setCacheObject(phonenumber+Constants.REGISTER , RandomUtil.randomNumbers(4),58, TimeUnit.SECONDS);

                redisCache.setCacheObject(phoneCode.getPhonenumber()+Constants.REGISTER , "9527",58, TimeUnit.SECONDS);
                break;
            //登录
            case 1 :
                if(StringUtils.isNotNull(redisCache.getCacheObject(phoneCode.getPhonenumber()+Constants.LOGIN))){
                    msg = "一分钟内只能请求一次验证码，请等候~~";
                    break;
                }
//        redisCache.setCacheObject(phonenumber+Constants.LOGIN , RandomUtil.randomNumbers(4),58, TimeUnit.SECONDS);

                redisCache.setCacheObject(phoneCode.getPhonenumber()+Constants.LOGIN , "9527",58, TimeUnit.SECONDS);

                break;
            //备案验证
            case 2 :
                if(StringUtils.isNotNull(redisCache.getCacheObject(phoneCode.getPhonenumber()+Constants.FOR_RECORD))){
                    msg = "一分钟内只能请求一次验证码，请等候~~";
                    break;
                }
//        redisCache.setCacheObject(phonenumber+Constants.LOGIN , RandomUtil.randomNumbers(4),58, TimeUnit.SECONDS);

                redisCache.setCacheObject(phoneCode.getPhonenumber()+Constants.FOR_RECORD , "9527",58, TimeUnit.SECONDS);

                break;
            //绑定验证
            case 3 :
                if(StringUtils.isNotNull(redisCache.getCacheObject(phoneCode.getPhonenumber()+Constants.BINDING))){
                    msg = "一分钟内只能请求一次验证码，请等候~~";
                    break;
                }
//        redisCache.setCacheObject(phonenumber+Constants.LOGIN , RandomUtil.randomNumbers(4),58, TimeUnit.SECONDS);

                redisCache.setCacheObject(phoneCode.getPhonenumber()+Constants.BINDING , "9527",58, TimeUnit.SECONDS);

                break;
            //重置密码
            case 4 :
                if(StringUtils.isNotNull(redisCache.getCacheObject(phoneCode.getPhonenumber()+Constants.RESET))){
                    msg = "一分钟内只能请求一次验证码，请等候~~";
                    break;
                }
//        redisCache.setCacheObject(phonenumber+Constants.LOGIN , RandomUtil.randomNumbers(4),58, TimeUnit.SECONDS);

                redisCache.setCacheObject(phoneCode.getPhonenumber()+Constants.RESET , "9527",58, TimeUnit.SECONDS);

                break;
            default:
                msg =  "未定义发送验证码类型";
                break;
        }
        return msg;
    }
}
